﻿using System;
using System.ComponentModel.DataAnnotations;
using AutoMapper;
using Modules;
using Modules.Attachment;
using Modules.Project;
using PmSoft;
using PmSoft.Caching;

namespace ManagementApi.Models.Project
{
    /// <summary>
    /// 项目解上锁记录
    ///</summary>
    public class ProjectLockLogEditForm : ProjectLockLog
    {
        /// <summary>
        /// 申请解锁原因
        ///</summary>
        [Display(Name = "申请解锁原因")]
        public string? UnlockReason { get; set; }

        /// <summary>
        /// 附件
        /// </summary>
        [Display(Name = "附件")]
        public IEnumerable<AttachmentsForm<int>>? Attachments { get; set; }

        #region 扩展字段

        //这里自行增加扩展字段，如 项目名称

        public int Month { get; set; }
        public string? ProjectName { get; set; }

        public string? AuditStatusName { get; set; }

        public string? AuditIdeaName { get; set; }

        #endregion 扩展字段
    }

    public static class ProjectLockLogExtensions
    {
        public static ProjectLockLogEditForm AsForm(this ProjectLockLog entity)
        {
            ProjectLockLogEditForm form = new();
            var mapper = ServiceAccessor.GetService<IMapper>();
            mapper.Map(entity, form);

            #region 扩展属性赋值

            var projectService = ServiceAccessor.GetService<ProjectService>();
            form.ProjectName = projectService.GetProjectInfo(entity.ProjectId)?.Name ?? string.Empty;
            form.Month = entity.LockMonth;

            form.AuditStatusName = form.AuditStatus?.GetDescription() ?? string.Empty;

            form.AuditIdeaName = form.AuditIdea?.GetDescription() ?? string.Empty;

            #endregion 扩展属性赋值

            return form;
        }
    }
}